<?php
namespace app\admin\controller;

use app\admin\validate\Task as TaskValidate;
use think\exception\ValidateException;
use app\model\Task as TaskModel;
use app\model\OnJob;

class Task extends Auth
{
    /**
     * 任务列表
     *
     * @return void
     */
    public function list()
    {
        $where[] = ['delete_time', '=', null];
        // 岗位名称
        $work_name = input('work_name');
        if (!empty($work_name)) {
            $where[] = ['work_name', 'like', "%{$work_name}%"];
        }
        // 公司名称
        $company_name = input('company_name');
        if (!empty($company_name)) {
            $where[] = ['company_name', 'like', "%{$company_name}%"];
        }
        // 申请区域 省
        $address_sheng = input('address_sheng');
        if (!empty($address_sheng)) {
            $where[] = ['address_sheng', '=', $address_sheng];
        }
        // 申请区域 城
        $address_shi = input('address_shi');
        if (!empty($address_shi)) {
            $where[] = ['address_shi', '=', $address_shi];
        }
        // 创建时间
        $start_time = input('start_time');
        $end_time = input('end_time');
        if (!empty($start_time) && !empty($end_time)) {
            $end_time = date('Y-m-d', (strtotime($end_time) + 86399));
            $where[] = ['create_time', 'between', "{$start_time}, {$end_time}"];
        }
        // 是否上线 0上线 1下线
        $is_show = input('is_show');
        if (!empty($is_show)) {
            $where[] = ['is_show', '=', $is_show];
        }

        $page = input('page');
        if (empty($page)) {
            $page = 1;
        }
        $limit = input('limit');
        if (empty($limit)) {
            $limit = 10;
        }
        $page_count = ($page - 1) * $limit;

        $list = TaskModel::where($where)
            ->field('id, work_name, pay_min, pay_max, sex, age_min, age_max, recruit_num, company_name, address_sheng, address_shi, address_details, is_show, create_time')
            ->limit($page_count, $limit)
            ->order('create_time desc')
            ->select();
        $count = TaskModel::where($where)
            ->count();
        success('任务列表', ['data' => $list, 'count' => $count, 'page' => $page]);
    }

    /**
     * 添加任务
     *
     * @return void
     */
    public function create()
    {
        $task_data = [
            'work_name'       => input('work_name'),
            'pay_min'         => input('pay_min'),
            'pay_max'         => input('pay_max'),
            'recruit_num'     => input('recruit_num'),
            'tag'             => input('tag'),
            'welfare'         => input('welfare'),
            'sex'             => input('sex'),
            'age_min'         => input('age_min'),
            'age_max'         => input('age_max'),
            'work_desc'       => input('work_desc'),
            'company_name'    => input('company_name'),
            'address_sheng'   => input('address_sheng'),
            'address_shi'     => input('address_shi'),
            'address_details' => input('address_details'),
            'work_env'        => input('work_env'),
            'word'            => input('word')
        ];

        // 校验数据
        try {
            validate(TaskValidate::class)->check($task_data);
        } catch (ValidateException $e) {
            error($e->getError());
        }

        $res = TaskModel::createTask($task_data);

        if ($res === true) {
            success('创建成功');
        } else {
            error('创建失败');
        }
    }

    /**
     * 查看任务详情
     *
     * @return void
     */
    public function view()
    {
        $id = input('id');
        if (empty($id)) {
            error('缺少参数');
        }

        $info = TaskModel::where('id', $id)->where('delete_time', null)->find();
        success('任务详情', $info);
    }

    /**
     * 修改任务
     *
     * @return void
     */
    public function update()
    {
        $task_data = [
            'work_name'       => input('work_name'),
            'pay_min'         => input('pay_min'),
            'pay_max'         => input('pay_max'),
            'recruit_num'     => input('recruit_num'),
            'tag'             => input('tag'),
            'welfare'         => input('welfare'),
            'sex'             => input('sex'),
            'age_min'         => input('age_min'),
            'age_max'         => input('age_max'),
            'work_desc'       => input('work_desc'),
            'company_name'    => input('company_name'),
            'address_sheng'   => input('address_sheng'),
            'address_shi'     => input('address_shi'),
            'address_details' => input('address_details'),
            'work_env'        => input('work_env'),
            'word'            => input('word')
        ];

        // 校验数据
        try {
            validate(TaskValidate::class)->check($task_data);
        } catch (ValidateException $e) {
            error($e->getError());
        }
        $id = input('id');
        if (empty($id)) {
            error('缺少参数');
        }
        $res = TaskModel::updateTask($id, $task_data);

        if ($res === true) {
            success('创建成功');
        } else {
            error('创建失败');
        }
    }

    /**
     * 任务上线、下线
     *
     * @return boolean
     */
    public function isShow()
    {
        $id = input('id');
        if (empty($id)) {
            error('缺少参数');
        }
        $res = TaskModel::isShow($id);
        success($res);
    }

    /**
     * 删除任务
     *
     * @return void
     */
    public function delete()
    {
        $id = input('id');
        if (empty($id)) {
            error('缺少参数');
        }

        $res = TaskModel::where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s', time())]);
        if ($res) {
            success('删除成功');
        } else {
            error('删除失败');
        }
    }

    /**
     * 人才列表
     *
     * @return void
     */
    public function staffList()
    {
        $task_id = input('id');
        if (!empty($task_id)) {
            $where[] = ['task_id', '=', $task_id];
        } else {
            error('缺少参数');
        }
        $type = input('type');
        if (!empty($type)) {
            $where[] = ['type', '=', $type];
        }
        $name = input('name');
        if (!empty($name)) {
            $where[] = ['name', 'like', "%{$name}%"];
        }

        $page = input('page');
        if (empty($page)) {
            $page = 1;
        }
        $limit = input('limit');
        if (empty($limit)) {
            $limit = 10;
        }
        $page_count = ($page - 1) * $limit;

        $list = OnJob::where($where)->limit($page_count, $limit)->select();
        $count = OnJob::where($where)->count();
        success('人才列表', ['data' => $list, 'count' => $count, 'page' => $page]);
    }

    /**
     * 录用、不录用人才
     *
     * @return void
     */
    public function setStaffType()
    {
        $id = input('id');
        $type = input('type'); // 1 同意入职 2 拒绝入职
        if (empty($id) || empty($type)) {
            error('参数错误');
        }
        if ($type == 1) {
            $staff_id = OnJob::where('id', $id)->value('staff_id');
            $is_on_job = OnJob::where('staff_id', $staff_id)->where('type', 1)->find();
            if ($is_on_job) {
                error('已入职其他任务, 无法入职');
            } else {
                OnJob::where('id', $id)->update(['type' => $type, 'entry_time' => date('Y-m-d H:i:s', time())]);
            }
        } elseif ($type == 2) {
            OnJob::where('id', $id)->update(['type' => $type]);
        }

        success('操作成功');
    }
}